<template>
  <div>
    <el-form label-width="100px">
      <el-form-item label="SKU名称">
        <el-input
          placeholder="请输入SKU名称"
          v-model="skuParams.skuName"
        ></el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input
          placeholder="价格(元)"
          type="number"
          v-model="skuParams.price"
        ></el-input>
      </el-form-item>
      <el-form-item label="重量(克)">
        <el-input
          placeholder="重量(克)"
          type="number"
          v-model="skuParams.weight"
        ></el-input>
      </el-form-item>
      <el-form-item label="SKU描述">
        <el-input
          placeholder="请输入SKU描述"
          type="textarea"
          v-model="skuParams.skuDesc"
        ></el-input>
      </el-form-item>
      <el-form-item label="平台属性">
        <el-form inline>
          <el-form-item
            v-for="(item, index) in attrInfoList"
            :key="item.id"
            :value="item.id"
            :label="item.attrName"
          >
            <el-select style="width: 200px" v-model="item.attrIdAndValueId">
              <el-option
                v-for="attrValue in item.attrValueList"
                :key="attrValue.id"
                :value="`${item.id}:${attrValue.id}`"
                :label="attrValue.valueName"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-form inline>
          <el-form-item
            v-for="(item, index) in spuSaleAttrList"
            :key="item.id"
            :value="item.id"
            :label="item.saleAttrName"
          >
            <el-select style="width: 200px" v-model="item.saleIdAndValueId">
              <el-option
                v-for="spuSaleAttrValue in item.spuSaleAttrValueList"
                :key="spuSaleAttrValue.id"
                :value="`${item.id}:${spuSaleAttrValue.id}`"
                :label="spuSaleAttrValue.saleAttrValueName"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片名称">
        <el-table ref="imgTableRef" :data="spuImageList" border>
          <el-table-column
            type="selection"
            width="80"
            align="center"
          ></el-table-column>
          <el-table-column label="图片">
            <template #="{ row, $index }">
              <img
                :src="row.imgUrl"
                alt=""
                style="width: 100px; height: 100px"
              />
            </template>
          </el-table-column>
          <el-table-column label="名称" prop="imgName"></el-table-column>
          <el-table-column label="操作">
            <template #="{ row, $index }">
              <el-button
                type="primary"
                size="small"
                @click="handleDefault(row)"
              >
                设置默认
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="default" @click="handleSave">
          保存
        </el-button>
        <el-button size="default" icon="" @click="handleCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts" name="SkuForm">
import { reactive, ref } from 'vue'
import { getAttrInfoListApi } from '@/api/product/attr'
import {
  getSpuImageListApi,
  getSpuSaleAttrListApi,
  addOrUpdateSkuApi,
} from '@/api/product/spu'
import { SkuData, SpuData } from '@/api/product/spu/type'
import { ElMessage } from 'element-plus'

const $emit = defineEmits(['changeScene'])
const attrInfoList = ref<any>([])
const spuImageList = ref<any>([])
const spuSaleAttrList = ref<any>([])
const skuParams = reactive<SkuData>({
  // 父组件传递过来的数据
  category3Id: '', //三级分类的ID
  spuId: '', //已有的SPU的ID
  tmId: '', //SPU品牌的ID
  // v-model收集
  skuName: '', //sku名字
  price: '', //sku价格
  weight: '', //sku重量
  skuDesc: '', //sku的描述
  skuAttrValueList: [],
  skuSaleAttrValueList: [],
  skuDefaultImg: '', //sku图片地址
})
// 获取图片表格ref
const imgTableRef = ref<any>()
// 方法
const initSkuData = async (
  category1Id: number | string,
  category2Id: number | string,
  spuData: SpuData,
) => {
  // 收集数据
  skuParams.category3Id = spuData.category3Id
  skuParams.spuId = spuData.id
  skuParams.tmId = spuData.tmId
  // 获取属性列表
  const res: any = await getAttrInfoListApi(
    category1Id,
    category2Id,
    spuData.category3Id,
  )
  // 获取SPU图片列表
  const res1: any = await getSpuImageListApi(spuData.id)
  // 获取SPU销售属性列表
  const res2: any = await getSpuSaleAttrListApi(spuData.id)

  attrInfoList.value = res.data
  spuImageList.value = res1.data
  spuSaleAttrList.value = res2.data
}
const handleSave = async () => {
  // 整理参数
  skuParams.skuAttrValueList = attrInfoList.value.reduce(
    (prev: any, next: any) => {
      if (next.attrIdAndValueId) {
        const [attrId, valueId] = next.attrIdAndValueId.split(':')
        prev.push({
          attrId,
          valueId,
        })
      }
      return prev
    },
    [],
  )

  skuParams.skuSaleAttrValueList = spuSaleAttrList.value.reduce(
    (prev: any, next: any) => {
      if (next.saleIdAndValueId) {
        const [saleAttrId, saleAttrValueId] = next.saleIdAndValueId.split(':')
        prev.push({
          saleAttrId,
          saleAttrValueId,
        })
      }
      return prev
    },
    [],
  )
  // 发起请求
  const res = await addOrUpdateSkuApi(skuParams)
  if (res.code == 200) {
    ElMessage.success('添加SKU成功')
    // 切换场景
    $emit('changeScene', { scene: 0, pageFlag: 'update' })
  } else {
    ElMessage.error('添加SKU失败')
  }
}
const handleCancel = () => {
  $emit('changeScene', { scene: 0, pageFlag: '' })
}
// 设置默认图片方法
const handleDefault = (row: any) => {
  // 取消所有复选框
  imgTableRef.value.clearSelection()
  // 复选框选中
  imgTableRef.value.toggleRowSelection(row, true)
  // 收集图片地址
  skuParams.skuDefaultImg = row.imgUrl
}

defineExpose({ initSkuData })
</script>

<style scoped lang="scss"></style>
